package componentes;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.media.opengl.GL;

import transformaciones.TAfin;
import utils.math.MathFlowers;
import figuras.BSplines;
import figuras.malla.Malla;

public class Componente3D {

	protected Color color;
	protected TAfin matriz;
	protected GL gl;
	protected ArrayList<BSplines> bsplines;
	protected Malla malla;

	public Componente3D() {
		
	}

	public Componente3D(GL gl, TAfin matriz, Malla malla, Color color) {
		this.gl = gl;
		this.matriz = matriz;
		this.malla = malla;
		this.color = color;
	}

	public void dibuja(int modo) {
		gl.glPushMatrix();
		gl.glMultMatrixd(matriz.getMatriz());
		double red = color.getRed() / 255.0;
		double green = color.getGreen() / 255.0;
		double blue = color.getBlue() / 255.0;
		gl.glColor3d(red, green, blue);
		if (malla != null)
			malla.dibuja(modo, gl);
		gl.glPopMatrix();
	}

	public TAfin getMatriz() {
		if (matriz != null)
			return matriz;
		else
			return new TAfin(gl);
	}

	public void setMatriz(TAfin matriz) {
		this.matriz = matriz;
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public ArrayList<BSplines> getBsplines() {
		return bsplines;
	}

	public void setBsplines(ArrayList<BSplines> bsplines) {
		this.bsplines = bsplines;
	}

	public Malla getMalla() {
		return malla;
	}

	public void setMalla(Malla malla) {
		this.malla = malla;
	}

	public void setGl(GL gl) {
		this.gl = gl;
	}

	public GL getGl() {
		return gl;
	}
	
	public void saveToBinaryPLY(String path) {
		malla.saveToBinaryPLY(matriz,path, this.getClass().toString());
	}

	public void saveToAsciiPLY(String path) {
		malla.saveToAsciiPLY(matriz,path, this.getClass().toString());
	}
	
	public void save(String file){
		PrintWriter out;
		try {
			out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
			save(out);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void save(PrintWriter out){
		malla.save(out);
	}
	
	public void load(String file){
		BufferedReader in;
		try {
			in = new BufferedReader(new FileReader(file));
			load(in);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void load(BufferedReader in){
		malla.load(in);
	}
	
	public void setTexture(int texture) {
		malla.setTexture(texture);
		malla.calculaPuntosDeTextura();
	}

	public void setTextureBlended(boolean blended) {
		malla.setBlendTexture(blended);		
	}

	public void removeTexture() {
		malla.setTexture(-1);		
	}
	
	protected int getPlyNumVertices(){
		return malla.getNVertices();
	}
	
	protected int getPlyNumCaras(){
		return malla.getNCaras();
	}
	
	protected void writePlyVertex(TAfin matriz,PrintWriter out){
		TAfin m = new TAfin(gl);
		m.getMatriz().put(MathFlowers.multiplicar(matriz.getMatriz().array(),this.matriz.getMatriz().array()));
		getMalla().writeVertex(m,out);
	}
	
	protected void writePlyFaces(int count,PrintWriter out){
		getMalla().writeFace(out, count);
	}
	
}
